package com.dk.service.impl;

import com.dk.mapper.MenuMapper;
import com.dk.mapper.RoleMapper;
import com.dk.mapper.RoleMenuMapper;
import com.dk.pojo.Menu;
import com.dk.service.MenuService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
     @Autowired
     private RedisTemplate redisTemplate;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    public List<Map<String, Object>> getInfo() {
        List<Menu> list = menuMapper.selectByExample(null);
        List<Map<String, Object>> result = getMenuTree(list, 0);
        return result;
        //先去redis中获取数据，如果redis中存在数据直接返回，如果不存在查询数据库
        //并保存在redis中
        //定义redis使用的key
       /*
          第一种方式：不适应数据量大的问题，已string方式进行获取
       String key = "menu:info" + uid;
        ValueOperations strRedis = redisTemplate.opsForValue();
        if(redisTemplate.hasKey(key)){//说明redis中存在数据
            String menuValue = (String) strRedis.get(key);
            List<Map<String, Object>> resultList= new Gson().fromJson(
                    menuValue, new TypeToken<List<Map<String, Object>>>(){}.getType());
            return resultList;

        }else {//不存在 查询数据库
             List<Menu> list = menuMapper.selectInfo(uid);
            List<Map<String, Object>> result = getMenuTree(list, 0);
            String resultStr = new Gson().toJson(result);
            strRedis.set(key,resultStr);
            return result;
        }*/
        /*第二种已hash方式进行获取*/
       /* HashOperations hashRedis = redisTemplate.opsForHash();
        String key = "permission";
        String field = "menu:info:" + uid;
        boolean b = hashRedis.hasKey(key, field);
        if (b){//redis中存在数据
            List<Map<String, Object>> result = (List<Map<String, Object>>)hashRedis.hasKey(key, field);
            return result;
        }else {
            List<Menu> list = menuMapper.selectInfo(uid);
            List<Map<String, Object>> result = getMenuTree(list, 0);
            hashRedis.put(key,field,result);
            return result;
        }
*/
    }

    @Override
    public List<Map<String, Object>> getInfozTree() {
        List<Menu> list = menuMapper.selectByExample(null);
        List<Map<String, Object>> result = getMenuzTree(list, 0);
        return result;
    }

    @Override
    public Boolean insertMenuInfo(Menu menu) {
        return menuMapper.insertSelective(menu) > 0;

    }

    @Override
    public Boolean deleteById(Integer id) {
        return menuMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public Menu selectById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>>  getInfoZtreeChecked(Integer rid) {
        List<Menu> menuList = menuMapper.selectByExample(null);
        List<Integer> mids = roleMenuMapper.selectById(rid);
        List<Map<String, Object>> infoZtreeChecked = getInfoZtreeChecked(menuList, 0, mids);
        return infoZtreeChecked;
    }
    public  List<Map<String,Object>>  getInfoZtreeChecked(List<Menu> list,Integer id,List<Integer> mids){
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //传递的主键id和当前pid是否相等
            Menu menu = list.get(i);
            if (id.equals(menu.getPid())) {
                map =  new HashMap<>();
                map.put("name",menu.getText());
                map.put("url",menu.getUrl());
                map.put("id",menu.getId());
                map.put("children",getInfoZtreeChecked(list,menu.getId(),mids));
                if (mids != null && mids.size() > 0 && mids.contains(menu.getId())){
                    map.put("checked",true);
                }
            }
            if(map != null){
                List<Map<String,Object>> list1 = (List<Map<String, Object>>) map.get("children");
                if (list1.size() <= 0){
                    map.remove("children");
                }
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public Boolean update(Menu menu) {
        return menuMapper.updateByPrimaryKeySelective(menu) > 0 ;

    }

    public  List<Map<String,Object>>  getMenuTree(List<Menu> list,Integer id){
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //传递的主键id和当前pid是否相等
            Menu menu = list.get(i);
            if (id.equals(menu.getPid())) {
                map =  new HashMap<>();
                map.put("text",menu.getText());
                map.put("url",menu.getUrl());
                map.put("id",menu.getId());
                map.put("nodes",getMenuTree(list,menu.getId()));
            }
            if(map != null){
               List<Map<String,Object>> list1 = (List<Map<String, Object>>) map.get("nodes");
               if (list1.size() <= 0){
                   map.remove("nodes");
               }
                result.add(map);
            }
        }
        return result;
    }
    public  List<Map<String,Object>>  getMenuzTree(List<Menu> list,Integer id){
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            //传递的主键id和当前pid是否相等
            Menu menu = list.get(i);
            if (id.equals(menu.getPid())) {
                map =  new HashMap<>();
                map.put("name",menu.getText());
                map.put("url",menu.getUrl());
                map.put("id",menu.getId());
                map.put("children",getMenuzTree(list,menu.getId()));
            }
            if(map != null){
                List<Map<String,Object>> list1 = (List<Map<String, Object>>) map.get("children");
                if (list1.size() <= 0){
                    map.remove("children");
                }
                result.add(map);
            }
        }
        return result;
    }
}
